package com.example.park2301

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.example.lib_base.mvvm.BaseViewActivity
import com.example.park2301.databinding.ActivitySplashBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask
@AndroidEntryPoint
class SplashActivity : BaseViewActivity<ActivitySplashBinding>() {
    var timer:Timer?=null
    var num =5
    override fun initView() {
        //二次进入不显示
        if (SPUtils.getInstance().getBoolean("isIn",false)){
            ARouter.getInstance().build("/app/UserActivity").navigation()
            finish()
        }
//        timer=Timer()
//        lifecycleScope.launch (Dispatchers.IO){
//            timer!!.schedule(timerTask {
//                lifecycleScope.launch {
//                    binding.tvTime.setText("${num}s")
//                    if (num==0){
//                        ks()
//                    }
//                    num--
//                }
//            },0,1000)
//        }
        binding.tvSplash.setOnClickListener {
            ks()
        }
        timer=Timer()
        lifecycleScope.launch {
            timer!!.schedule(timerTask {
                                       lifecycleScope.launch {
                                           binding.tvTime.setText("${num}s" )
                                           if (num==0){
                                               ks()
                                           }
                                           num--
                                       }
            }
            ,0,1000)
        }
    }

    private fun ks() {
        timer!!.cancel()
        SPUtils.getInstance().put("isIn",true)
        ARouter.getInstance().build("/app/UserActivity").navigation()
        finish()
    }

    override fun initData() {

    }

    override fun onDestroy() {
        super.onDestroy()
        if (timer!=null){
            timer?.cancel()
            timer=null
        }
    }

}